//上传图片到七牛
function uploadFile(obj, type){
        var file = obj.files[0];
        var name = file.name;
        var imgtype = file.name.split('.')[1];
        var reader = new FileReader();
        reader.onload = function(e){
        var imgBase64Data = e.target.result;
        var pos = imgBase64Data.indexOf("4")+2;
        imgBase64Data = imgBase64Data.substring(pos, imgBase64Data.length - pos);
       $.ajax({
           url:"/qiniu/upToken.json",
           type:"get",
            dataType:"json",
           success:function(data){
                var uptoken = data.data.uptoken;
               var qiniuUploadUrl;
               if (window.location.protocol === 'https:') {
                   qiniuUploadUrl = 'https://up.qbox.me';
               } else {
                   qiniuUploadUrl = 'http://upload.qiniu.com';
               }
                    $.ajaxFileUpload({
                        url:qiniuUploadUrl,
                      secureuri:false,
                      fileElementId: "fileUpload"+type,
                      dataType:'json',
                        type:'post',
                        timeout:30000,
                        contentType:"multipart/form-data",
                      data:{
                            'type':type,
                            'token':uptoken,
                            'chunk':'0',
                            'chunks':'1',
                            'key':md5(imgBase64Data)+'.'+imgtype,
                            'file':name
                        },
                        success:function(data){
                            console.log(type);
                            $('#myImg'+type).css('display','block');
                            $('#myImg'+type).attr('src',$('#myImg1').attr('class') +"/"+ data.key);
                            $(".add-img"+type).css('display','none');
                            $(".add-span"+type).css('display','none');
                        },
                        error:function(error){
                            $('.info-err').html(error.statusText+',请检查您的文件大小')
                        }
                    });
                },
                error: function(err) {
                    $('.info-err').html(JSON.parse(err.responseText).errors[0].msg);
                }
            })  
    }
    reader.readAsDataURL(file);
    return false;  
}
//提交认证
$('.verify-btn').click(function(){
    var identityReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    var name = $('.realname').val();
    var identity = $('.identity').val();
    var idCardFacade = $('#myImg1').attr('src');
    var idCardReverse = $('#myImg2').attr('src');
    var idCardHandheld = $('#myImg3').attr('src');
    if(!name){
        $('.info-err').html('请输入您的真实姓名');
        return false;
    }
    if(!identity){
        $('.info-err').html('请输入您的身份证号');
        return false;
    }
    if(!idCardFacade){
        $('.info-err').html('请输入身份证正面照片');
        return false;
    }
    if(!idCardReverse){
        $('.info-err').html('请输入身份证反面照片');
        return false;
    }
    if(!idCardHandheld){
        $('.info-err').html('请输入身份证手持照片');
        return false;
    }
    if(!$('.read input').attr('checked')){
        $('.info-err').html('接受投资协议才可实名认证');
        return false;
    }
    var data = {
        name:name,
        idNo:identity,
        idCardFacade:idCardFacade, 
        idCardReverse:idCardReverse,
        idCardHandheld:idCardHandheld

    };
    $.ajax({
       url:"/user/auth.json",
       type:"post",
        data:JSON.stringify(data),
          contentType:'application/json;charset=UTF-8',
       success:function(data){
           $('.info-err').html('提交成功！请您等待审核');
        },
        error: function(err) {
            $('.info-err').html(JSON.parse(err.responseText).errors[0].msg);
        }
    })
});

